package com.cloud.product.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.common.entity.ShopProduct;
import com.cloud.product.service.ShopProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 产品 前端控制器
 * </p>
 *
 * @author 李均华
 * @since 2021-06-04
 */
@RestController
@RequestMapping("/shop/product")
public class ShopProductController {

    @Autowired
    private ShopProductService shopProductService;

    /**
     * 减去库存
     * @param id 商品id
     * @param num 数量
     * @return 下单数量
     */
    @GetMapping("/reduceProduct/{id}/{num}")
    public Integer reduceProduct(@PathVariable("id") Integer id, @PathVariable("num") Integer num){
        ShopProduct shopProduct = shopProductService.getById(id);
        shopProduct.setNum(shopProduct.getNum()-num).updateById();
        return shopProduct.getNum();
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/page")
    public IPage<ShopProduct> page(){
        Page<ShopProduct> page = new Page<>();
        return shopProductService.page(page);
    }
}

